//
//  UserInfoViewController.m
//  Wujin
//
//  Created by cz on 2016/12/12.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "UserInfoViewController.h"

@interface UserInfoViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *qqTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *wechatTextField;
@property (weak, nonatomic) IBOutlet UITextField *prePhoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;


@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //给个返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goback)];
    [self setupPersonalInfo];
}

//设置个人信息
- (void)setupPersonalInfo {
    
    if ([WJUser user].name == NULL || [WJUser user].qq == NULL || [WJUser user].wechat == NULL || [WJUser user].pre_phone == NULL || [WJUser user].address == NULL) {
        
        [self goback];
        return;
    }
    
    self.nameTextField.text = [WJUser user].name;
    self.qqTextFiled.text = [WJUser user].qq;
    self.wechatTextField.text = [WJUser user].wechat;
    self.prePhoneTextField.text = [WJUser user].pre_phone;
    self.addressTextField.text = [WJUser user].address;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self setupPersonalInfo];
}

//修改个人信息
- (IBAction)modifyInfo {
    
    
    if (self.nameTextField.text.length == 0 || self.prePhoneTextField.text.length == 0 || self.addressTextField.text.length == 0) {
        [MRHud showWithFailure:@"必选项不能为空"];
        return;
    }
    
    NSString *url = F(@"%@User/edit", BASEURL);
    
    NSDictionary *userInfo = @{
                               @"qq":self.qqTextFiled.text,
                               @"edit_address":self.addressTextField.text,
                               @"username":self.nameTextField.text,
                               @"pre_phone":self.prePhoneTextField.text,
                               @"user_id":[WJUser user].user_id,
                               @"wechat":self.wechatTextField.text
                 };
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSDictionary *dict = @{
                           @"data":jsonStr
                           };
    
    [FSServer requestWithTarget:url param:dict result:^(id responseObject, NSError *error) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *d = responseObject[@"data"];
            if ([responseObject[@"flag"] intValue] == 1 && d.count != 0) {
                
                [MRHud showWithSuccess:@"修改成功"];
                [WJUser user].name = self.nameTextField.text;
                [WJUser user].qq = self.qqTextFiled.text;
                [WJUser user].wechat = self.wechatTextField.text;
                [WJUser user].pre_phone = self.prePhoneTextField.text;
                [WJUser user].address = self.addressTextField.text;

                [self goback];
            }else {
                [MRHud showWithFailure:@"修改失败，请重新修改"];
            }
            
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
    
}

- (IBAction)goback {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
